using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Belmost4PDA.Constants;
using Belmost4PDA.Fasade;

namespace Belmost4PDA.Logic
{
    public class Control_Grid:AbstractControl
    {
        private readonly List<Control_GridItem> LGI=new List<Control_GridItem>();
        private readonly ListView LW=new ListView();
        public string orderBy = "";

        public Control_Grid()
        {
          
        }
        public override string GetControlType()
        {
            return ControlType.GridGroup;
        }

        public override AbstractMapper GetMapper()
        {
            return null;
        }

        public override Control GetFormControl()
        {
            LW.Columns.Clear();
            foreach (AbstractControl control in ChildControls)
            {
                if (ControlType.GridItem == control.GetControlType())
                    LW.Columns.Add(((Control_GridItem)control).GetListViewControl());
                if (ControlType.GridCLSItem == control.GetControlType())
                    LW.Columns.Add(((Control_GridItem)control).GetListViewControl());

            }
            LW.View = View.Details;
            LW.FullRowSelect = true;
            LW.SelectedIndexChanged+=ListView_SelectedIndexChanged;
            return LW;
        }

        public override string GetCaption()
        {
            return "";
        }

        public virtual string GetIdFieldName()
        {
            foreach (AbstractControl control in ChildControls)
            {
                if (ControlType.GridIdItem == control.GetControlType())
                    return control.GetMapper().FieldName();
            }
            return null;
        }

        public virtual List<Control_GridItem> GetColumns()
        {
            foreach (AbstractControl control in ChildControls)
            {
                LGI.Add((Control_GridItem)control);
            }
            return LGI;
        }
        public virtual void ClearLines()
        {
           LW.Items.Clear();
        }
        public virtual void AddLine(DataRows.DataRows rows)
        {
            ListViewItem lwi=null;
            int i = 0;
            foreach (AbstractControl control in ChildControls)
            {
                 if(ControlType.GridItem==control.GetControlType())
                 {
                     if (i == 0)
                         lwi = new ListViewItem(rows.FieldByNameDef(control.GetMapper().FieldName(), ""));
                     else lwi.SubItems.Add(rows.FieldByNameDef(control.GetMapper().FieldName(), ""));
                     i++;
                 }
                 if (ControlType.GridCLSItem == control.GetControlType())
                 {
                     if (i == 0)
                         lwi =
                             new ListViewItem(
                                 ((Mapper_GridCLSItem)(control.GetMapper())).FieldVal(
                                     rows.FieldByNameDef(control.GetMapper().FieldName(), "")));
                     else
                         lwi.SubItems.Add(((Mapper_GridCLSItem) (control.GetMapper())).FieldVal(
                                              rows.FieldByNameDef(control.GetMapper().FieldName(), "")));
                     i++;
                 }               
            }

            lwi.Tag = new CmdReadData(rows.FieldByNameDef(GetIdFieldName(), ""), GetIdFieldName());
            LW.Items.Add(lwi);
        }

        public static void ListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem o in ((ListView)sender).Items)
            {
                if (o.Selected) InterfaceDispatcher.ExecCommand((AbstractCommand) o.Tag);
            }
        }

    }
}
